<template>
	<z-paging ref="paging" v-model="dataList" @query="queryList"
		:loading-more-custom-style="{ paddingBottom: 'env(safe-area-inset-bottom)' }">
		<template #top>
			<view class="zpagin_top">
				<nav-bar :iconLeft="true" />
				<view class="search">
					<custom-search v-model="queryParams.search" @search="refreshList" placeholder="请输入用户姓名" />
					<view style="display: flex; gap: 10px;">
						<uni-data-select style="z-index: 50;" placeholder="请选择部门" v-model="queryParams.group_id"
							:localdata="group_list" @change="refreshList" />
						<uni-data-select style="z-index: 50;" placeholder="请选择分类" v-model="queryParams.classify"
							:localdata="classify_list" @change="refreshList" />
					</view>
				</view>
				<dui-float-ball :pathUrl="`${pageUrl}/compile`" v-if="$hasPerm(['90301'])" />
			</view>
		</template>
		<up-swipe-action>
			<up-swipe-action-item v-for="item in dataList" :key="item.id" :item-data="item"
				:name="JSON.stringify(swipeActPer(options, item.status_display)) + '&' + item.id + '&' + item.full_name "
				:options="swipeActPer(options, item.status_display)"
				:disabled="swipeActPer(options, item.status_display, 'disabled')" @click="action">
				<uni-card :title="item.full_name" :extra="item.classify_display" margin="0" spacing="0"
					@click="goDetail(item, pageUrl, '90304')">
					<view class="con">
						<view class="ctent">手机：{{ item.mobile }}</view>
						<view class="ctent">部门：{{ item.group_name }}</view>
					</view>
					<view class="con">
						<view class="ctent">民族：{{ item.nation }}</view>
						<view class="ctent">职务：{{ item.role_names }}</view>
					</view>
					<view class="con">
						<view class="ctent">性别：{{ item.sex_display }}</view>
						<view class="ctent">工龄(年)：{{ item.years }}</view>
					</view>
					<view class="con">
						<view class="ctent">紧急联系人：{{ item.urgency_full_name }}</view>
						<view class="ctent">紧急电话：{{ item.urgency_mobile }}</view>
					</view>
					<view class="con" style="align-items: normal;">
						<view class="ctent" style="flex: none;">备注：</view>
						<view class="ctent">{{ item.desc }}</view>
					</view>
				</uni-card>
			</up-swipe-action-item>
		</up-swipe-action>
	</z-paging>
</template>
<script setup>
import { ref, reactive } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import acUserApi from '../../../api/account/ac_user'
import { goDetail, swipeActPer } from '@/utils/permission'

//作用于新增或编辑后刷新页面数据
uni.$bus.useBusOn('ac_user', refreshList);

//页面header标题

onLoad(option => {
	get_group_list()
})

//滑动单元格按钮
const options = [
	{
		text: '修改', per: '90302',
		style: {
			backgroundColor: '#0081ff',
		}
	}, {
		text: '删除', per: '90303',
		style: {
			backgroundColor: '#f56c6c'
		}
	}
];
//页面跳转路径
const pageUrl = '/views/account/ac_user';

const classify_list = [
	{ text: '正式员工', value: 1 },
	{ text: '临时员工', value: 2 },
	{ text: '试用员工', value: 3 },
	{ text: '离职员工', value: 4 },
]

//查询参数
const queryParams = reactive({
	search: '',
	group_id: '',
	classify: '',
});

//获取paging实例
const paging = ref(null)
const dataList = ref([])
//列表数据方法
function queryList(pageNo, pageSize) {
	const params = {
		page: pageNo,
		page_size: pageSize,
		...queryParams
	}
	acUserApi.get_staff(params).then((res) => {
		paging.value?.complete(res.results);
	})
}

const group_list = ref([]);
function get_group_list() {
	acUserApi.get_group({ page: 1, page_size: 99999 }).then(res => {
		group_list.value = res.results.map(v => ({
			text: v.name,
			value: v.id
		}))
	})
};

//滑动操作按钮
function action(v) {
	const options = JSON.parse(v.name.split("&")[0])
	const id = v.name.split("&")[1]
	const name = v.name.split("&")[2]
	const index = v.index
	if (options[index].text == '修改') {
		uni.navigateTo({
			url: `${pageUrl}/compile?type=edit&id=${id}`
		})
	}
	if (options[index].text == '删除') {
		delete_type(id, name)
	}
};

//删除操作
function delete_type(id, name) {
	uni.showModal({
		title: '提示',
		content: `确认要删除【${name}】的员工吗？`,
		success: (res) => {
			if (res.confirm) {
				acUserApi.delete_staff(id).then((res) => {
					uni.$u.toast('删除成功')
					setTimeout(() => {
						refreshList();
					}, 500)
				})
			}
		}
	});
};

//刷新列表数据方法
function refreshList() {
	paging.value?.reload()
};
</script>

<style lang="scss">

</style>